/**
 * diamonds
 * spades
 * hearts
 * clubs
 * $(document).ready(function() - doing at start
 */


Array.prototype.sum = function(){
	for(var i=0,sum=0;i<this.length;sum+=this[i++]);
	return sum;
};

//all cards by numbers
var car = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 , 17, 18, 19, 20, 21, 22, 23,
           24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
           45, 46, 47, 48, 49, 50, 51];

//links to the cards images 
var cardlinks =["images/cards/d/d2.jpg", "images/cards/d/d3.jpg", "images/cards/d/d4.jpg",
                "images/cards/d/d5.jpg", "images/cards/d/d6.jpg", "images/cards/d/d7.jpg",
                "images/cards/d/d8.jpg", "images/cards/d/d9.jpg", "images/cards/d/dT.jpg",
                "images/cards/d/dJ.jpg", "images/cards/d/dQ.jpg", "images/cards/d/dK.jpg",
                "images/cards/d/dA.jpg", "images/cards/s/s2.jpg", "images/cards/s/s3.jpg",
                "images/cards/s/s4.jpg", "images/cards/s/s5.jpg", "images/cards/s/s6.jpg",
                "images/cards/s/s7.jpg", "images/cards/s/s8.jpg", "images/cards/s/s9.jpg",
                "images/cards/s/sT.jpg", "images/cards/s/sJ.jpg", "images/cards/s/sQ.jpg",
                "images/cards/s/sK.jpg", "images/cards/s/sA.jpg", "images/cards/h/h2.jpg",
                "images/cards/h/h3.jpg", "images/cards/h/h4.jpg", "images/cards/h/h5.jpg",
                "images/cards/h/h6.jpg", "images/cards/h/h7.jpg", "images/cards/h/h8.jpg",
                "images/cards/h/h9.jpg", "images/cards/h/hT.jpg", "images/cards/h/hJ.jpg",
                "images/cards/h/hQ.jpg", "images/cards/h/hK.jpg", "images/cards/h/hA.jpg",
                "images/cards/c/c2.jpg", "images/cards/c/c3.jpg", "images/cards/c/c4.jpg",
                "images/cards/c/c5.jpg", "images/cards/c/c6.jpg", "images/cards/c/c7.jpg",
                "images/cards/c/c8.jpg", "images/cards/c/c9.jpg", "images/cards/c/cT.jpg",
                "images/cards/c/cJ.jpg", "images/cards/c/cQ.jpg", "images/cards/c/cK.jpg",
                "images/cards/c/cA.jpg"];
//player banks(money the've got)			
var banks = [100, 100, 100, 100, 100, 100, 100, 100, 100];
//bets that players make
var plbet = [0, 0, 0, 0, 0, 0, 0, 0, 0];
//True if the player didn't fold the cards
var ingame = [1, 1, 1, 1, 1, 1, 1, 1, 1];

var dealer=0;
var move = 0;

var t = 9;

var sb = 2;//small blind
var bb = 4;//big blind
var bank = 0;
var bet = 4;

var counter = 0;

var k = 0;//?

var anal = [0, 0, 0, 0, 0]; // anal[0], anal[1] 1st/2nd card low = 1, medium = 2, high = 3,
//anal[2] connection (0, 1, 2, or 3). anal[3] suited(1) or not (0)
//anal[4] pair(1) or not(0)

var plcards = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var flopcards = [0, 0, 0, 0, 0];

//When the document loads, poker starts immediately
function startgame(){
	/*jQuery.fx.off = true; //Turn off animations*/
	//Randomly Choose the first dealer
	var z = (Math.floor(Math.random()*8 + 1));
	prepreflop();
	dealers(z);
	$("#bank").html(bank);
	preflop();
};

$(document).ready(function() {

	//Is activated when document loads
	addyourname();//change name
	animatetable();
	setTimeout('startgame()',500);
});


function check(){
	//check if all active players have same bets, if round complete - next turn
	var a=0;
	var b=0;
	for (i=0; i < ingame.length; i++){
		if (ingame[i]==1 && plbet[i]==bet){
			a+=1;
		}
		if (ingame[i]==1){
			b+=1;
		}
	}
	if (a == b){
		return 1;	
	}
	else {
		return 0;
	}

}



function sum(array){
	sum = 0;
	for (i=0; i < array.length; i++){
		sum = sum + array[i];
	}
	return sum;
}


function preflop() {
	k+=3;
	setTimeout('plmove()',5000);
}

function hidebuttons(){
	//Hide all buttons from the player, so that he cannot affect the game while it is not his turn
	$("#bet").css("opacity", "0");
	$("#bet").attr("onClick", "");
	$("#fold").css("opacity", "0");
	$("#fold").attr("onClick", "");
	$("#call").css("opacity", "0");
	$("#call").attr("onClick", "");
	$("#raise").css("opacity", "0");
	$("#raise").attr("onClick", "");
	$("#check").css("opacity", "0");
	$("#check").attr("onClick", "");
}

function flop(){
	move = 1;
	
	bank = bank + sum(plbet);
	plbet = [0, 0, 0, 0, 0, 0, 0, 0, 0];
	bet = 0;
	$("#bank").html(bank);
	$("div.playerbet").css("opacity", "0");
	t_card1 = random();
	t_card2 = random();
	t_card3 = random();
	flopcards[0]=t_card1;
	flopcards[1]=t_card2;
	flopcards[2]=t_card3;
	$("img#t_card1").attr("src", cardlinks[flopcards[0]]);
	$("img#t_card2").attr("src", cardlinks[flopcards[1]]);
	$("img#t_card3").attr("src", cardlinks[flopcards[2]]);
	dealtcards(1,3);

	k=dealer+1;
	setTimeout('plmoveflop()',3000);
}



function plmoveflop(){
	var time=1000;
	if($("#pl"+k+"_state").html()=="FOLD"){
		time=0;
		counter += 1;
		k+=1;
		setTimeout('plmoveflop()',time);

	}
	if (k >= 10){
		k = k-9;
	}
	var id="#player"+k;
	if (k == 1){
		if($("#pl1_state").html()!="FOLD"){
			pl1moveflop();
		}
	}

	else if($("#pl"+k+"_state").html()!="FOLD"){
		activeplayer(id);
		result = flopanalys(plcards[k*2-1], plcards[k*2-2]);

		//raise
		if (result == 2){			
			if (bet == bb*4){
				banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
				plbet[k-1] = bet;//player's bet
				$("#pl"+k+"_bank").html(banks[k-1]);
				$("div#pl_"+k+"_bet").html(plbet[k-1]);
				$("div#pl_"+k+"_bet").css("opacity", "1");
				addlog(k,"calls");
			}
			else {
				bet = bet + bb; // bet raises per 1 bb	
				banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
				plbet[k-1] = bet;	//player's bet
				$("#pl"+k+"_bank").html(banks[k-1]);
				$("div#pl_"+k+"_bet").html(plbet[k-1]);
				$("div#pl_"+k+"_bet").css("opacity", "1");
				addlog(k,"raises");
			}
		}
		//call
		else if (result == 1){
			banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
			plbet[k-1] = bet;//player's bet
			$("#pl"+k+"_bank").html(banks[k-1]);
			$("div#pl_"+k+"_bet").html(plbet[k-1]);
			$("div#pl_"+k+"_bet").css("opacity", "1");
			addlog(k,"calls");
		}
		//fold
		else if (result == 0){
			if (bet == 0){
				banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
				plbet[k-1] = bet;//player's bet
				$("#pl"+k+"_bank").html(banks[k-1]);
				$("div#pl_"+k+"_bet").html(plbet[k-1]);
				$("div#pl_"+k+"_bet").css("opacity", "1");
				addlog(k,"checks");
			}
			else{
				$("#pl"+k+"_state").html("FOLD");
				returnacard("#player"+k+"_1");
				returnacard("#player"+k+"_2");
				addlog(k,"fold");
				ingame[k-1]=0;
			}

		}
		if($("#pl"+k+"_state").html()!="FOLD"){
			notactiveplayer(id);
		}
	}

	if (check() == 1 && counter>=9 && move == 1){
		turn();
	}
	if (check() == 1 && counter>=9 && move == 2){
		river();
	}
	else{
		counter += 1;
		k+=1;
		setTimeout('plmoveflop()',time);
	}



}


function prepreflop(){
	//Deals the player cards
	i=0;
	while (i<plcards.length){
		plcards[i]=random();
		i++;
	}
	$("img#player1_1").attr("src", cardlinks[plcards[0]]);
	$("img#player1_2").attr("src", cardlinks[plcards[1]]);
	dealcards(1,1);
	hidebuttons();

	//for debugging, show computer player cards 

	$("img#player2_1").attr("src", cardlinks[plcards[2]]);
	$("img#player2_2").attr("src", cardlinks[plcards[3]]);
	$("img#player3_1").attr("src", cardlinks[plcards[4]]);
	$("img#player3_2").attr("src", cardlinks[plcards[5]]);
	$("img#player4_1").attr("src", cardlinks[plcards[6]]);
	$("img#player4_2").attr("src", cardlinks[plcards[7]]);
	$("img#player5_1").attr("src", cardlinks[plcards[8]]);
	$("img#player5_2").attr("src", cardlinks[plcards[9]]);
	$("img#player6_1").attr("src", cardlinks[plcards[10]]);
	$("img#player6_2").attr("src", cardlinks[plcards[11]]);
	$("img#player7_1").attr("src", cardlinks[plcards[12]]);
	$("img#player7_2").attr("src", cardlinks[plcards[13]]);
	$("img#player8_1").attr("src", cardlinks[plcards[14]]);
	$("img#player8_2").attr("src", cardlinks[plcards[15]]);
	$("img#player9_1").attr("src", cardlinks[plcards[16]]);
	$("img#player9_2").attr("src", cardlinks[plcards[17]]);
}



function random(){
	//chooses a random card from an array and deletes it from the array
	var a = (Math.floor(Math.random()*car.length));
	b = car[a];
	car.splice(a, 1);
	return b;
}

function plmove(){
	//The AI of the computer
	u= ingame.sum();
	if (u==1){
		endgame();
	}
	var time=1000;
	if($("#pl"+k+"_state").html()=="FOLD"){
		time=0;
	}
	if (k >= 10){
		k = k-9;
	}
	var id="#player"+k;

	if (k == 1){
		if($("#pl1_state").html()!="FOLD"){
			activeplayer(id);
			pl1move();
		}
	}
	else if($("#pl"+k+"_state").html()!="FOLD"){
		activeplayer(id);
		analys(plcards[k*2-1], plcards[k*2-2]);
		result = anal.join("");


		if ((anal[0]==3) && (anal[1]==3)){			
			if (bet == bb*4){
				banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
				plbet[k-1] = bet;//player's bet
				$("#pl"+k+"_bank").html(banks[k-1]);
				$("div#pl_"+k+"_bet").html(plbet[k-1]);
				$("div#pl_"+k+"_bet").css("opacity", "1");
				addlog(k,"calls");
			}
			else {
				bet = bet + bb; // bet raises per 1 bb	
				banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
				plbet[k-1] = bet;	//player's bet
				$("#pl"+k+"_bank").html(banks[k-1]);
				$("div#pl_"+k+"_bet").html(plbet[k-1]);
				$("div#pl_"+k+"_bet").css("opacity", "1");
				addlog(k,"raises");
			}
		}
		else if ((result == 22110) || (result == 32110) || (result == 22001) || (result == 23110)){
			if (bet == bb*4){
				banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
				plbet[k-1] = bet;//player's bet
				$("#pl"+k+"_bank").html(banks[k-1]);
				$("div#pl_"+k+"_bet").html(plbet[k-1]);
				$("div#pl_"+k+"_bet").css("opacity", "1");
				addlog(k,"calls");
			}
			else {
				bet = bet + bb; // bet raises per 1 bb	
				banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
				plbet[k-1] = bet;	//player's bet
				$("#pl"+k+"_bank").html(banks[k-1]);
				$("div#pl_"+k+"_bet").html(plbet[k-1]);
				$("div#pl_"+k+"_bet").css("opacity", "1");
				addlog(k,"raises");
			}
		}
		else if ((result == 11001) || (result == 23310) || (result == 32310) || (result == 23310) || (result == 11110)|| (result == 22210) || (result == 32210)){
			banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
			plbet[k-1] = bet;//player's bet
			$("#pl"+k+"_bank").html(banks[k-1]);
			$("div#pl_"+k+"_bet").html(plbet[k-1]);
			$("div#pl_"+k+"_bet").css("opacity", "1");
			addlog(k,"calls");
		}

		else {
			$("#pl"+k+"_state").html("FOLD");
			returnacard("#player"+k+"_1");
			returnacard("#player"+k+"_2");
			addlog(k,"fold");
			t = t-1;
			ingame[k-1]=0;
		}
		if($("#pl"+k+"_state").html()!="FOLD"){
			notactiveplayer(id);
		}
	}
	if (check() == 1 && dealer!=k){
		flop();
	}
	else{
		k+=1;
		setTimeout('plmove()',time);
	}

}



function pl1move() {
	//Move of the player

	$("#bank").html(t);
	if (t==1){
		endgame();
	}
	else{
		if (check() == 1 && dealer!=k && move == 0){
			flop();
		}

		if (plbet[0] == bet){
			$("#check").css("opacity", "1");
			$("#check").attr("onClick", "plcheck()");
			$("#raise").css("opacity", "1");
			$("#raise").attr("onClick", "plraise()");
		}
		if (plbet[0] < bet && plbet[0] != bb*4){
			$("#fold").css("opacity", "1");
			$("#fold").attr("onClick", "plfold()");
			$("#call").css("opacity", "1");
			$("#call").attr("onClick", "plcall()");
			$("#raise").css("opacity", "1");
			$("#raise").attr("onClick", "plraise()");
		}
		if (plbet[0] == bb*4){
			//if the bet is maksimum, goes on
			hidebuttons();
			return
		}		
		wait();
	}
}

function pl1moveflop(){
	if (plbet[0] == bet){
		$("#check").css("opacity", "1");
		$("#check").attr("onClick", "plcheck()");
		$("#raise").css("opacity", "1");
		$("#raise").attr("onClick", "plraise()");
	}
	if (plbet[0] < bet && plbet[0] != bb*4){
		$("#fold").css("opacity", "1");
		$("#fold").attr("onClick", "plfold()");
		$("#call").css("opacity", "1");
		$("#call").attr("onClick", "plcall()");
		$("#raise").css("opacity", "1");
		$("#raise").attr("onClick", "plraise()");
	}
	if (plbet[0] == bb*4){
		//if the bet is maksimum, goes on
		hidebuttons();
		return
	}		
	wait();
}


function plraise(){
	//Raise players bet
	if (bet == bb*4){
		plcall();
	}
	else {
		bet = bet + bb; // bet raises per 1 bb	
		banks[0] = banks[0] - bet + plbet[0];// player's bank
		plbet[0] = bet;	//player's bet
		$("#pl1_bank").html(banks[0]);
		$("div#pl_1_bet").html(plbet[0]);
		$("div#pl_1_bet").css("opacity", "1");	
	}
	hidebuttons();
	notactiveplayer("#player1");
	k=2;
	addlog(1,"raises");
	if (move == 0){
		setTimeout('plmove()',1000);
	}
	else if (move == 1 || move == 2){
		setTimeout('plmoveflop()',1000);
	}
	else if (move == 3){
		setTimeout('lastmove()',1000);
	}

}

function plcall(){
	//Same bet as the other players
	banks[0] = banks[0] - bet + plbet[0];// player's bank
	plbet[0] = bet;	//player's bet
	$("#pl1_bank").html(banks[0]);
	$("div#pl_1_bet").html(plbet[0]);
	$("div#pl_1_bet").css("opacity", "1");

	hidebuttons();
	notactiveplayer("#player1");
	k=2;
	addlog(1,"calls");
	if (move == 1 || move == 2){
		setTimeout('plmoveflop()',1000);
	}
	else if (move == 3){
		setTimeout('lastmove()',1000);
	}
	else{
		setTimeout('plmove()',1000);
	}
}

function plcheck(){
	//Do nothing
	hidebuttons();
	notactiveplayer("#player1");
	k=2;
	addlog(1,"checks");
	if (move == 1 || move == 2){
		setTimeout('plmoveflop()',1000);
	}
	else if (move == 3){
		setTimeout('lastmove()',1000);
	}
	else{
		setTimeout('plmove()',1000);
	}
}

function plfold(){
	//Drop cards
	$("#pl1_state").html("FOLD");
	returnacard("#player1_1");
	returnacard("#player1_2");

	hidebuttons();
	if($("#pl1_state").html()!="FOLD"){
		notactiveplayer("#pl1_state");
	}
	ingame[0]=0;
	k=2;
	addlog(1,"fold");
	if (move == 1 || move == 2){
		setTimeout('plmoveflop()',1000);
	}
	else if (move == 3){
		setTimeout('lastmove()',1000);
	}
	else{
		setTimeout('plmove()',1000);
	}
}

function dealers(a) {
	//Sets a dealer and compulsory bets (small blind and big blind)
	$("img#dealer"+a).css("opacity", "1");//Shows, who is the dealer
	dealer=a;
	k=a;
	if (a==9){
		a=0;
	}
	banks[a] = banks[a]-sb;
	plbet[a] = plbet[a] + sb;
	$("div#pl_"+(a+1)+"_bet").html(plbet[a]);//
	$("#pl"+(a+1)+"_bank").html(banks[a]);
	$("div#pl_"+(a+1)+"_bet").css("opacity", "1");
	if (a==8){
		a=-1;
	}
	$("div#pl_"+(a+2)+"_bet").html(bb);
	$("#pl"+(a+2)+"_bank").html(banks[a+1]-bb);
	banks[a+1] = banks[a+1]-bb;
	plbet[a+1] = plbet[a+1] + bb;
	$("div#pl_"+(a+2)+"_bet").css("opacity", "1");
}

function analys(card1, card2){
	//Card analysys of the computer players
	if ((card1%13 <= 4) && (card1%13 >= 0)){
		anal[0] = 1;
	}
	if ((card2%13 <= 4) && (card2%13 >= 0)){
		anal[1] = 1;
	}

	if ((card1%13 <= 9) && (card1%13 >= 5)){
		anal[0] = 2;
	}
	if ((card2%13 <= 9) && (card2%13 >= 5)){
		anal[1] = 2;
	}

	if ((card1%13 <= 12) && (card1%13 >= 10)){
		anal[0] = 3;
	}
	if ((card2%13 <= 12) && (card2%13 >= 10)){
		anal[1] = 3;
	}

	if (card1%13 == card2%13){
		anal[4]=1;
		anal[2]=0;
		anal[3]=0;

	}
	else{
		if (card1<=12 && card2 <=12){
			anal[3]=1;
		}
		else if ((card1<=25 && card1>=13) && (card2 <=25 && card2>=13)){
			anal[3]=1;
		}
		else if ((card1<=38 && card1>=26) && (card2 <=38 && card2>=26)){
			anal[3]=1;
		}
		else if ((card1<=51 && card1>=39) && (card2 <=51 && card2>=39)){
			anal[3]=1;
		}
		else{
			anal[3]=0;
		}
		if (Math.abs(card1%13-card2%13)==1){
			anal[2]=1;
		}
		else if (Math.abs(card1%13-card2%13)==2){
			anal[2]=2;
		}
		else if (Math.abs(card1%13-card2%13)==3){
			anal[2]=3;
		}
		else{
			anal[2]=0;
		}

	}

}

function turn(){
	counter = 0;
	move = 2;
	plbet = [0, 0, 0, 0, 0, 0, 0, 0, 0];
	bet = 0;
	bb = bb*2;
	$("#bank").html(bank);
	$("div.playerbet").css("opacity", "0");
	t_card4 = random();
	flopcards[3]=t_card4;
	$("img#t_card4").attr("src", cardlinks[flopcards[3]]);
	dealtcards(4,4);
	k=dealer+1;
	setTimeout('plmoveflop()',3000);
}

function river(){
	counter = 0;
	move = 3;
	plbet = [0, 0, 0, 0, 0, 0, 0, 0, 0];
	bet = 0;
	$("#bank").html(bank);
	$("div.playerbet").css("opacity", "0");
	t_card5 = random();
	flopcards[4]=t_card5;
	$("img#t_card5").attr("src", cardlinks[flopcards[4]]);
	k=dealer+1;
	dealtcards(5,5);
	setTimeout('lastmove()',3000);
}

function lastmove(){
	var time=1000;
	if($("#pl"+k+"_state").html()=="FOLD"){
		time=0;
	}
	if (k >= 10){
		k = k-9;
	}
	var id="#player"+k;

	if (k == 1){
		if($("#pl1_state").html()!="FOLD"){
			activeplayer(id);
			pl1move();
		}
	}
	else if($("#pl"+k+"_state").html()!="FOLD"){
		activeplayer(id);
		if (bet == bb*4){
			banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
			plbet[k-1] = bet;//player's bet
			$("#pl"+k+"_bank").html(banks[k-1]);
			$("div#pl_"+k+"_bet").html(plbet[k-1]);
			$("div#pl_"+k+"_bet").css("opacity", "1");
		}
		else {
			bet = bet + bb; // bet raises per 1 bb	
			banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
			plbet[k-1] = bet;	//player's bet
			$("#pl"+k+"_bank").html(banks[k-1]);
			$("div#pl_"+k+"_bet").html(plbet[k-1]);
			$("div#pl_"+k+"_bet").css("opacity", "1");
		}
	}
	if (check() == 1 && counter>=9 && move == 3){
		endgame();
	}
	else{
		counter += 1;
		k+=1;
		setTimeout('lastmove()',time);
	}
}

function endgame(){
	wait();
}

function sortNumber(a,b)
{
	return a - b;
}

function flopanalys(card1, card2){
	cards = [0, 0, 0, 0, 0];
	cards[0] = flopcards[0]%13;
	cards[1] = flopcards[1]%13;
	cards[2] = flopcards[2]%13;
	cards[3] = card1%13;
	cards[4] = card2%13;

	cards = cards.sort(sortNumber);
	//trips or 4th
	if (((cards[0]==cards[1])&&(cards[1]==cards[2])) || ((cards[1]==cards[2])&&(cards[2]==cards[3])) || ((cards[2]==cards[3])&&(cards[3]==cards[4]))){
		return 2;//raise
	}
	// straight or straight flush or royal flush
	else if ((cards[4]-cards[0])== 4){
		return 2; 
	}
	//flush
	else if (card1<=12 && card2 <=12 && flopcards[0]<=12 && flopcards[1]<=12 && flopcards[2]<=12){
		return 2;
	}
	else if ((card1<=25 && card2<=25 && flopcards[0]<=25 && flopcards[1]<=25 && flopcards[2]<=25) && (card1>=12 && card2 >=12 && flopcards[0]>=12 && flopcards[1]>=12 && flopcards[2]>=12)){
		return 2;
	}
	else if ((card1>=26 && card2>=26 && flopcards[0]>=26 && flopcards[1]>=26 && flopcards[2]>=26) && (card1<=38 && card2 <=38 && flopcards[0]<=38 && flopcards[1]<=38 && flopcards[2]<=38)){
		return 2;
	}
	else if ((card1>=39 && card2>=39 && flopcards[0]>=39 && flopcards[1]>=39 && flopcards[2]>=39) && (card1<=51 && card2 <=51 && flopcards[0]<=51 && flopcards[1]<=51 && flopcards[2]<=51)){
		return 2;
	}
	//top pair or over pair or 2 pair
	else if ((card1%13==card2%13) || (card1%13==flopcards[2]%13) || (card2%13==flopcards[2]%13)){
		return 2;//call
	}
	//straight dro (2)
	else if (((cards[4]-cards[1])==3) && ((cards[3]-cards[0])==3)){
		return 1;// call
	}
	else{
		return 0;// fold
	}

}